﻿using Mapster;
using MapsterMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using ZR.Model.Business;
using ZR.Model.ThemeModels;

namespace ZR.Infrastructure.ANet
{
    /// <summary>
    /// Mapster映射帮助类 -基础使用
    /// Mapster 版本7.3.0
    /// ASP.NET使用时可直接services.AddMapster();
    /// </summary>
    public class MapsterHelper
    {
        #region 实体映射
        /// <summary>
        /// 1.1、类型映射_默认字段一一对应
        /// T需要映射后的实体 = 需要映射的实体.Adapt<T需要映射后的实体>();
        /// </summary>
        /// <typeparam name="TSource">源类型</typeparam>
        /// <typeparam name="TDestination">目标类型</typeparam>
        /// <param name="tSource">源数据</param>
        /// <returns></returns>
        public static TDestination MapsterTo<TSource, TDestination>(TSource tSource) where TSource : class where TDestination : class
        {
            if (tSource == null) return default;

            return tSource.Adapt<TDestination>();
        }
        /// <summary>
        /// 1.2、类型映射_默认字段一一对应 (映射到现有对象)
        /// T需要映射后的实体 = 需要映射的实体.Adapt<T需要映射后的实体>();
        /// </summary>
        /// <typeparam name="TSource">源类型</typeparam>
        /// <typeparam name="TDestination">目标类型</typeparam>
        /// <param name="tDestination">目标对象</param>
        /// <param name="tSource">源数据</param>
        /// <returns></returns>
        public static TDestination MapsterTo<TSource, TDestination>(TDestination tDestination, TSource tSource) where TSource : class where TDestination : class
        {
            if (tSource == null) return default;

            return tSource.Adapt(tDestination);
        }

        /// <summary>
        /// 2、类型映射
        /// ① 字段名称不对应
        /// ② 类型转化
        /// ③ 字段省略
        /// ④ 字段名称或类型不对应
        /// ⑤ 条件赋值或null处理
        /// ⑥ 组合赋值
        /// </summary>
        /// <typeparam name="TSource">源类型</typeparam>
        /// <typeparam name="TDestination">目标类型</typeparam>
        /// <param name="tSource">源数据</param>
        /// <param name="configurationExpression">类型</param>
        /// <returns></returns>
        public static TDestination MapsterTo<TSource, TDestination>(TSource tSource, TypeAdapterConfig typeAdapterConfig) where TSource : class where TDestination : class
        {
            if (tSource == null) return default;

            //var typeAdapterConfig = new TypeAdapterConfig();
            //typeAdapterConfig.ForType<MapsterTestTable_ViewModel, MapsterTestTable>()
            //    .Map(member => member.DestName, source => source.Name)  // 指定字段一一对应
            //    .Map(member => member.Birthday, source => source.Birthday.ToString("yy-MM-dd HH:mm"))                              // 指定字段，并转化指定的格式
            //    .Map(member => member.Age, source => source.Age > 5)                                                               // 条件赋值
            //    .Ignore(member => member.A1)                                                                                       // 忽略该字段，不给该字段赋值
            //    .IgnoreNullValues(true)                                                                                            // 忽略空值映射
            //    .IgnoreAttribute(typeof(DataMemberAttribute))                                                                      // 忽略指定特性的字段
            //    .Map(member => member.A3, source => source.Name + source.Age * 3 + source.Birthday.ToString("d"))                  // 可以自己随意组合赋值
            //    .NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);                                                            // 忽略字段名称的大小写

            var mapper = new Mapper(typeAdapterConfig);  // 可以自己随意组合赋值
            return mapper.Map<TDestination>(tSource);
        }
        #endregion 实体映射

        #region 列表映射
        /// <summary>
        /// 3、集合列表类型映射,默认字段名字一一对应
        /// </summary>
        /// <typeparam name="TSource">源类型</typeparam>
        /// <typeparam name="TDestination">目标类型</typeparam>
        /// <param name="source">源数据</param>
        /// <returns></returns>
        public static List<TDestination> MapsterToList<TSource, TDestination>(List<TSource> sources) where TSource : class where TDestination : class
        {
            if (sources == null) return new List<TDestination>();

            return sources.Adapt<List<TDestination>>();
        }
        #endregion 列表映射



        /// <summary>
        /// 土地所有权出让公告实体转粤公平主题库要求字段
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static V3TdsyqDealBehaviorInfoDescontext MapsterToTdsyqDealBehaviorDesc(V3TdsyqDealBehaviorInfo info)
        {
            V3TdsyqDealBehaviorInfoDescontext behaviorDesc = new();

            if(info != null)
            {
                var typeAdapterConfig = new TypeAdapterConfig();
                typeAdapterConfig.ForType<V3TdsyqDealBehaviorInfo, V3TdsyqDealBehaviorInfoDescontext>()
                    .Map(member => member.ANNOUNCEMENT_GUID, source => source.AnnouncementGuid)
                    .Map(member => member.LAND_PROJECT_NAME, source => source.LandProjectName)
                    .Map(member => member.LISTING_TYPE, source => source.ListingType)
                    .Map(member => member.ANNOUNCEMENT_TITLE, source => source.AnnouncementTitle)
                    .Map(member => member.UNIFIED_DEAL_CODE, source => source.UnifiedDealCode)
                    .Map(member => member.REGION_CODE, source => source.RegionCode)
                    .Map(member => member.TRA_PLACE_CODE, source => source.TraPlaceCode)
                    .Map(member => member.TRA_AGENCY_NAME, source => source.TraAgencyName)
                    .Map(member => member.TRA_AGENCY_CODE, source => source.TraAgencyCode)
                    .Map(member => member.CLASSIFY_CODE, source => source.ClassifyCode)
                    .Map(member => member.CN_PROJECT_TYPE, source => source.CnProjectType)
                    .Map(member => member.ANNOUNCEMENT_CONNECT, source => source.AnnouncementConnect)
                    .Map(member => member.LAND_DISTRICT, source => source.LandDistrict)
                    .Map(member => member.PROJECT_STATUS, source => source.ProjectStatus)
                    //创建时间和框架创建时间名称一样，推送取发布时间
                    .Map(member => member.CREATE_TIME, source => source.PublishingTime)
                    .Map(member => member.PUBLISHING_TIME, source => source.PublishingTime)
                    .Map(member => member.SUPPLY_TYPE, source => source.SupplyType)
                    .Map(member => member.CHANGE_TIME, source => source.ChangeTime)
                    .Map(member => member.RETREAT_TIME, source => source.RetreatTime)
                    .Map(member => member.ANNOUNCEMENT_UNIT, source => source.AnnouncementUnit)
                    .Map(member => member.END_DATE, source => source.EndDate)
                    .Map(member => member.NOTICE_NATURE, source => source.NoticeNature)
                    .Map(member => member.ANNOUNCEMENT_TYPE, source => source.AnnouncementType)
                    .Map(member => member.ANNOUNCEMENT_CODE, source => source.AnnouncementCode)
                    .Map(member => member.ANNOUNCEMENT_START_TIME, source => source.AnnouncementStartTime)
                    .Map(member => member.ANNOUNCEMENT_DEADLINE, source => source.AnnouncementDeadline)
                    .Map(member => member.LISTING_START_TIME, source => source.ListingStartTime)
                    .Map(member => member.LISTING_DEADLINE, source => source.ListingDeadline)
                    .Map(member => member.LIAISON_UNIT, source => source.LiaisonUnit)
                    .Map(member => member.CONTACT_PERSON, source => source.ContactPerson)
                    .Map(member => member.CONTACT_NUMBER, source => source.ContactNumber)
                    .Map(member => member.UNIT_ADDRESS, source => source.UnitAddress)
                    .Map(member => member.ZIP_CODE, source => source.ZipCode)
                    .Map(member => member.Email, source => source.Email)
                    .Map(member => member.CANCEL_REASON, source => source.CancelReason)
                    .Map(member => member.ACCOUNT_BANK, source => source.AccountBank)
                    .Map(member => member.BASIC_BANK, source => source.BasicBank)
                    .Map(member => member.BASIC_BRANCH_BANK, source => source.BasicBranchBank)
                    .Map(member => member.FIELD_NUM, source => source.FieldNum)
                    .Map(member => member.PASSED_APPLICANT_LIST, source => source.PassedApplicantList)
                    .Map(member => member.OTHER_CONDITIONS, source => source.OtherConditions)
                    .Map(member => member.BID_OPENING_ADDRESS, source => source.BidOpeningAddress)
                    .Map(member => member.IS_NETWORK_SYMBOL, source => source.IsNetworkSymbol)
                    .Map(member => member.REGISTRATION_URL, source => source.RegistrationUrl)
                    .Map(member => member.PLATFORM_CODE, source => source.PlatformCode)
                    .Map(member => member.PUB_SERVICE_PLAT_CODE, source => source.PubServicePlatCode)
                    .Map(member => member.DATA_TIMESTAMP, source => source.DataTimestamp)
                    .Map(member => member.BID_OPENING_ADDRESS, source => source.BidOpeningAddress)
                    .Map(member => member.BID_OPENING_ADDRESS, source => source.BidOpeningAddress)
                    .Map(member => member.BID_OPENING_ADDRESS, source => source.BidOpeningAddress)
                    .IgnoreNullValues(true); //忽略空值映射                                                                                      

                // 忽略该字段，不给该字段赋值     
                //.Ignore(member => member.A1)

                // 忽略指定特性的字段
                //.IgnoreAttribute(typeof(DataMemberAttribute)) 

                // 指定字段，并转化指定的格式
                //.Map(member => member.Birthday, source => source.Birthday.ToString("yy-MM-dd HH:mm")) 

                // 条件赋值
                //.Map(member => member.Age, source => source.Age > 5) 

                // 可以自己随意组合赋值
                //.Map(member => member.A3, source => source.Name + source.Age * 3 + source.Birthday.ToString("d"))   

                behaviorDesc = MapsterHelper.MapsterTo<V3TdsyqDealBehaviorInfo, V3TdsyqDealBehaviorInfoDescontext>(info, typeAdapterConfig);
            }


            return behaviorDesc;


        }


    }

    /// <summary>
    /// Mapster映射帮助类 - 示例（未测试）
    /// Mapster 版本12.0.0
    /// </summary>
    public class MapsterHelperTeatDemo
    {
        #region 实体映射
        /// <summary>
        /// 1、类型映射_默认字段一一对应
        /// </summary>
        public void MapsterToDemo()
        {
            MapsterTestTable_ViewModel tableTest1 = new();

            MapsterTestTable tableTest = MapsterHelper.MapsterTo<MapsterTestTable_ViewModel, MapsterTestTable>(tableTest1);
        }

        /// <summary>
        /// 2、类型映射
        /// ① 字段名称不对应
        /// ② 类型转化
        /// ③ 字段省略
        /// ④ 字段名称或类型不对应
        /// ⑤ 条件赋值或null处理
        /// ⑥ 组合赋值
        /// </summary>
        public void MapsterTo2Demo()
        {
            MapsterTestTable_ViewModel tableTest1 = new();
            // 这里是语句，不是无用的数据；
            var typeAdapterConfig = new TypeAdapterConfig();
            typeAdapterConfig.ForType<MapsterTestTable_ViewModel, MapsterTestTable>()
                .Map(member => member.DestName, source => source.Name)  // 指定字段一一对应
                .Map(member => member.Birthday, source => source.Birthday.ToString("yy-MM-dd HH:mm"))                              // 指定字段，并转化指定的格式
                .Map(member => member.Age, source => source.Age > 5)                                                               // 条件赋值
                .Ignore(member => member.A1)                                                                                       // 忽略该字段，不给该字段赋值
                .IgnoreNullValues(true)                                                                                            // 忽略空值映射
                .IgnoreAttribute(typeof(DataMemberAttribute))                                                                      // 忽略指定特性的字段
                .Map(member => member.A3, source => source.Name + source.Age * 3 + source.Birthday.ToString("d"))                  // 可以自己随意组合赋值
                .NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);                                                            // 忽略字段名称的大小写

            MapsterTestTable tableTest = MapsterHelper.MapsterTo<MapsterTestTable_ViewModel, MapsterTestTable>(tableTest1, typeAdapterConfig);
        }
        #endregion 实体映射

        #region 列表映射
        /// <summary>
        /// 3、集合列表类型映射,默认字段名字一一对应
        /// </summary>
        public void MapsterToListDemo()
        {
            List<MapsterTestTable_ViewModel> tableTestVMs = new();

            List<MapsterTestTable> tableTests = MapsterHelper.MapsterToList<MapsterTestTable_ViewModel, MapsterTestTable>(tableTestVMs);
        }
        #endregion 列表映射
    }

    #region MapsterHelper测试示例用
    public class MapsterTestTable_ViewModel
    {
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
        public int Age { get; set; }

        public DateTime A4 { get; set; }
    }

    // [AdaptTo("MapsterTestTable_ViewModel"), GenerateMapper]  // 显示声明式映射写法
    public class MapsterTestTable
    {
        public string DestName { get; set; }
        public DateTime Birthday { get; set; }
        public int Age { get; set; }

        //[AdaptIgnore]  // 显示声明式忽略该字段
        public string A1 { get; set; }
        public string A2 { get; set; }
        public string A3 { get; set; }
    }
    #endregion MapsterHelper测试示例用

}
